<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * 专家逻辑
 */
class Expertinroom extends LogicBase
{
    
    /**
     * 写入小屋专家关联表中（增加小屋专家）
     */
    public function expertinroomAdd($data=[])
    {
         $room_id=$data['room_id'];
         $url=url('expertinroomlist',['room_id'=>$room_id]);

        unset($data['room_id']);
       if (empty($data['expert']))
       {
         return [RESULT_SUCCESS, '专家列表空', $url];
      }
        $lst=$this->modelExpertinroom->getList(['room_id'=>$room_id],'id,expert_id');

        $val=array_extract($lst, 'expert_id');

        $list=[];
        foreach($data['expert'] as $k=>$v)
        {
            if(!in_array($v,$val))
        {
            $list[$k]=['expert_id'=>$v,'room_id'=>$room_id];
        }
        }

       
       if (empty($list))
       {
         return [RESULT_SUCCESS, '专家已在列表中', $url];
      }
         $result = $this->modelExpertinroom->setList($list);


        return  $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelExpertinroom->getError()];
    }

    /**
     * 获取某个健康小屋的专家信息
     */
    public function getExpertinroomList($room_id=0)
    {
        $this->modelExpertinroom->alias('er');

        $join=[['member m','er.expert_id=m.username'],['my_healthroom r','er.room_id=r.room_id']];
         $where=array('er.room_id'=>$room_id,'er.' . DATA_STATUS_NAME => ['neq', DATA_DELETE]);
         $field="er.*,m.nickname as expertname,r.name as roomname";
         $order='er.is_owner';
         $list=$this->modelExpertinroom->getList($where, $field,$order,$paginate = 0,$join);

         return $list;

    }
    
    /**
     * 专家信息编辑
     */
    public function expertinroomEdit($data = [])
    {
        
        $validate_result = $this->validateExpert->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateExpert->getError()];
        }
        
        $url = url('expertList');
        
        $result = $this->modelExpert->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '专家' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelExpert->getError()];
    }

    /**
     * 获取专家信息
     */
    public function getExpertinroomInfo($where = [], $field = true)
    {
        
        return $this->modelExpert->getInfo($where, $field);
    }
    
    /**
     * 专家删除
     */
    public function expertinroomDel($id=0,$room_id=0)
    {
        
        $result = $this->modelExpertinroom->deleteInfo(['id'=>$id]);
        
        $result && action_log('删除', '专家删除' . '，where：' . http_build_query(['id'=>$id]));

        // $url=url('expertinroomlist',['room_id'=>$room_id]);

        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelExpertinroom->getError()];
    }
    /**
     * 首页专家总数
     */
    public function getExperListForCount($where = [], $field = true, $order = '', $paginate = 20000)
    {
       
      $field='count(distinct expert_id) countnumber';
      $list=$this->modelExpertinroom->getList($where,$field,$order,$paginate);
       return $list['0']['countnumber'];
    }
}
